package edu.sjtu.grid.workflow.test;

import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Log {

	private static void write(String file, String text)
	{
		try{
			File f = new File(file);
			File dir = new File(f.getParent());
			if(!dir.exists())
				dir.mkdirs();
			if(!f.exists())
				f.createNewFile();
			FileWriter fw = new FileWriter(file, true);
			fw.append(text);
			fw.flush();
			fw.close();
		}catch (Exception e){
			System.err.println("Error: " + e.getMessage());
		}
	}
	
	private String logfile = null;
	private SimpleDateFormat format = null;
	
	public Log(String logfile)
	{
		this.logfile = logfile;
		this.format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	}
	
	public void write(String text)
	{
		String d = format.format(new Date());
		Log.write(this.logfile, "[" + d + "]\t" + text+"\r\n");
	}

}
